package class4;

import common.ArrayUtils;

/**
 * https://leetcode.cn/problems/number-of-even-and-odd-bits/description/
 * 奇偶位数
 */
public class Code07_EvenOddBit {
    public static void main(String[] args) {
        int n = 50;
        int[] ans = evenOddBit(n);
        ArrayUtils.printArr(ans);
        int[] ints = evenOddBit2(n);
        ArrayUtils.printArr(ints);

    }

    public static int[] evenOddBit(int n) {
        int i = 0, odd = 0, even = 0;
        while (n != 0) {
            if ((n & 1) == 1) {
                if (i % 2 == 0) {
                    even++;
                } else {
                    odd++;
                }
            }
            n >>= 1;
            i++;
        }
        return new int[]{even, odd};
    }

    public static int[] evenOddBit2(int n) {
        int[] ans = new int[2];
        for (int i = 0; n > 0; n >>= 1) {
            ans[i] += n & 1;
            i ^= 1; // 切换奇偶
        }
        return ans;
    }
}
